package HouseTest.Service;

import HouseTest.javaBean.Admin;
import HouseTest.javaBean.House;
import HouseTest.dao.HouseDao;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;


public class HouseService {
    private  ArrayList<House> houses = new ArrayList<>();
    HouseDao houseDao=new HouseDao();
    // 查找房屋
    public  boolean findHouse() throws IOException {

        System.out.println("请输入要查找的房屋编号: ");
       int a = HouseTest.Utility.readInt();
        House b=houseDao.findDAao(a);
        if(b!=null){
            System.out.println(b);
            return true;
        }
        System.out.println("未找到该房屋信息");
        return false;
    }
    // 更该信息
    public boolean changeHouse() throws IOException {
        System.out.println("请输入要修改的房屋编号: ");

        int a = HouseTest.Utility.readInt();
//修改前信息
        House b=houseDao.findDAao(a);
        House house=new House();
        house.setId(b.getId());
        getHouse(house);
            char c = HouseTest.Utility.readConfirmSelection();
            if (c == 'Y') {
                house.setState(false);
            } else {
                house.setState(true);
            }
        houseDao.revise(house);
        System.out.println("修改成功！！！");
            return true;
    }
    // 删除房屋
    public boolean deleteHouse() throws IOException {
        System.out.println("---------------------删除房屋---------------------");
        System.out.println("请输入要删除的房屋编号(-1退出): ");
        int a = HouseTest.Utility.readInt();
        House b=houseDao.findDAao(a);
        if (b==null){
            System.out.println("未添加房间，不能进行删除！！！");
            return false;
        }
        if(a == -1){
            System.out.println("---------------------退出删除---------------------");
            return false;
        }
            System.out.println("请确认是否删除(Y/N)请小心选择:");
            char c = HouseTest.Utility.readConfirmSelection();
            if(c == 'Y'){
                // 删除该编号的房间
                houseDao.deleteOneHouse(a);
                System.out.println("---------------------删除成功---------------------");
                return true;
            }else {
                System.out.println("---------------------删除失败---------------------");
                return false;
        }
    }
    // 添加房屋
    public boolean addHouse() throws IOException {
        System.out.println("---------------------添加房屋---------------------");
        House newHouse = new House();
        System.out.print("id: ");
        newHouse.setId(HouseTest.Utility.readInt(1));
        if(houseDao.findDAao(newHouse.getId())!=null){
            System.out.println("该房屋已有住户");
            return false;
        }
        getHouse(newHouse);
        System.out.println("---------------------添加完成---------------------");
        houseDao.insertHouse(newHouse);
        return true;
    }
    //输入房屋信息
    public void  getHouse(House newHouse){
        // 读取房主名字(1~4)位，不能为空
        System.out.print("姓名: ");
        newHouse.setName(HouseTest.Utility.readKeyBoard(4,false));
        // 读取房主的电话号码，小于等于11位
        System.out.print("电话: ");
        newHouse.setPhoneNumber(HouseTest.Utility.readKeyBoard(11,false));
        // 读取房子的地址(1~4)位，不能为空
        System.out.print("地址: ");
        newHouse.setAddress(HouseTest.Utility.readKeyBoard(4,false));
        // 读取房屋的月租，默认为3000
        System.out.print("月租: ");
        newHouse.setMoney(HouseTest.Utility.readInt(3000));
        // 读取房屋的状态
        System.out.print("状态(Y:未出租  N:已出租): ");
        char a = HouseTest.Utility.readConfirmSelection();
        newHouse.setState(a != 'Y');
    }
    // 展示所有房子信息
    public void listHouse() throws IOException {
   houses= houseDao.findList();
        System.out.println("---------------------房屋列表---------------------");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)");
        for (House value : houses) {
            System.out.println(value.toString());
        }
        System.out.println("---------------------房屋列表完成---------------------");
    }

    /**
     * 验证
     * @param admin
     * @return
     * @throws IOException
     */
    public boolean adminBoolean(Admin admin) throws IOException {
        Admin admin1 = houseDao.isAdmin(admin);
        return admin1 != null;
    }

    /**
     * 提供验证码
     * @return
     */
    public int CaptchaLogin(){
        // 创建Random对象
        Random random = new Random();
        // 生成一个四位数的随机验证码
        int captcha = 1000 + random.nextInt(9000); // 生成1000到9999之间的随机数
        System.out.println("生成的验证码为：" + captcha);
        return captcha;
    }
}

